﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QQ2564874169.Core.ORM;
using QQ2564874169.Core.ORM.Sqlserver;

namespace Tests.RelationalSql
{
    [TestClass]
    public class 增删改查操作:BaseTest
    {
        private MssqlExecute mssqlExecute(ISql sql)
        {
            return new MssqlExecute(sql);
        }

        [TestMethod]
        public void 如果在Before事件中设置了Result就会直接返回结果()
        {
            const int result = 5;
            using (var sql = new MockSql())
            {
                sql.Before += (s, e) =>
                {
                    ShowArgs(e);
                };
                sql.Rs = result;
                using (var execute = mssqlExecute(sql))
                {
                    var r = execute.Insert(new TestEntity { Id = 1, CreateTime = DateTime.Now, Name = "A" });
                    Assert.AreEqual(result, r);

                    execute.Before += (s, e) =>
                    {
                        e.Result = 1;
                    };
                    r = execute.Insert(new TestEntity { Id = 1, CreateTime = DateTime.Now, Name = "B" });
                    Assert.AreEqual(1, r);
                }
            }
        }

        [TestMethod]
        public void 展开IN参数()
        {
            var rs = false;
            using (var sql = new MockSql())
            {
                sql.Before += (s, e) =>
                {
                    //ShowDict(e);
                };
                sql.After += (s, e) =>
                {
                    rs = e.Context.Command == "where id in(@ids_0,@ids_1,@ids_2)";
                };
                sql.Query<TestEntity>("where id in @ids", new { ids = new List<int> { 1, 2, 3 } });
            }
            Assert.IsTrue(rs);

            using (var sql = new MockSql())
            {
                sql.Before += (s, e) =>
                {
                    ShowArgs(e);
                };
                sql.After += (s, e) =>
                {
                    rs = e.Context.Command == "where id in(@ids_0,@ids_1,@ids_2) and id=@id";
                };
                sql.Query<TestEntity>("where id in @ids and id=@id",
                    new { id = 1, ids = new List<int> { 1, 2, 3 } });
            }
            Assert.IsTrue(rs);

            using (var sql = new MockSql())
            {
                sql.Before += (s, e) =>
                {
                    ShowArgs(e);
                };
                sql.After += (s, e) =>
                {
                    rs = e.Context.Command == "where id=@id and id in(@ids_0,@ids_1,@ids_2)";
                };
                sql.Query<TestEntity>("where id=@id and id in @ids",
                    new { id = 1, ids = new List<int> { 1, 2, 3 } });
            }
            Assert.IsTrue(rs);

            using (var sql = new MockSql())
            {
                sql.Before += (s, e) =>
                {
                    ShowArgs(e);
                };
                sql.After += (s, e) =>
                {
                    rs = e.Context.Command == "where id=@id and id in(@ids_0,@ids_1,@ids_2) and oid in(@ids_0,@ids_1,@ids_2)";
                };
                sql.Query<TestEntity>("where id=@id and id in @ids and oid in @ids",
                    new { id = 1, ids = new List<int> { 1, 2, 3 } });
            }
            Assert.IsTrue(rs);
        }
    }
}
